package com.blockchain.repository;


import com.blockchain.model.Posts;
import com.blockchain.model.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface PostsRepository extends JpaRepository<Posts,Integer> {
    //按照时间降序排序，获取博客列表
    Page<Posts> findByAuthorOrderByCreatedAtDesc(User author, Pageable pageable);
    List<Posts> findByAuthorOrderByCreatedAtDesc(User author);
    Page<Posts> findAllByOrderByCreatedAtDesc(Pageable pageable);
    Page<Posts> findByTagsContainingOrderByCreatedAtDesc(String tags,Pageable pageable);
    @Query("SELECT p FROM Posts p WHERE p.title LIKE %:keyword% OR p.content LIKE %:keyword% OR p.author.username LIKE %:keyword%")
    Page<Posts> findByTitleOrContentOrAuthorUsernameContaining(String keyword, Pageable pageable);
    Posts findByPostId(int post_id);
    long countByAuthor(User user);
}
